﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

namespace SubwayHelper
{
    public static class Extend
    {
        /// <summary>
        /// 格式化当前字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string FormatTo(this string str, params object[] args)
        {
            return string.Format(str, args);
        }

        /// <summary>
        /// 忽略大小写比较两个字符串是否相等
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        public static bool IsSame(this string s1, string s2)
        {
            if (s1 == null || s2 == null) return false;
            return string.Equals(s1.Trim(), s2.Trim(), StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 返回集合的分隔符形式字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="spliter"></param>
        /// <returns></returns>
        public static string ToJoinString(this IEnumerable<string> source, string spliter = "-->")
        {
            return source == null ? null : string.Join(spliter, source);
        }

        /// <summary>
        /// 获得一个字符串的拼音
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToPinYin(this string str)
        {
            var sb = new StringBuilder();
            foreach (var item in str)
            {
                //判断是不是汉字，如果不是原字符返回
                if (ChineseChar.IsValidChar(item))
                {
                    sb.Append(GetPinYin(item));
                }
                else
                {
                    sb.Append(item);
                }
            }
            return sb.ToString().ToLower();
        }

        /// <summary> 
        /// 汉字转化为拼音首字母
        /// </summary> 
        /// <param name="str">汉字</param> 
        /// <returns>首字母</returns> 
        public static string ToFirstPinyin(this string str)
        {
            var r = string.Empty;
            foreach (var obj in str)
            {
                try
                {
                    if (obj == '广')
                    {
                        r += "g";
                        continue;
                    }
                    var chineseChar = new ChineseChar(obj);
                    var t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, 1);
                }
                catch
                {
                    r += obj.ToString();
                }
            }
            return r.ToLower();
        }

        /// <summary>
        /// 获得单个字符的拼音
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetPinYin(this char c)
        {
            if (c == '广') return "guang";
            var cc = new ChineseChar(c);
            var str = cc.Pinyins[0]; //多音字只取第一个
            return str.Substring(0, str.Length - 1); //去掉最后的声调
        }

        /// <summary> 
        /// 简体转换为繁体
        /// </summary> 
        /// <param name="str">简体字</param> 
        /// <returns>繁体字</returns> 
        public static string ToTraditional(this string str)
        {
            var r = ChineseConverter.Convert(str, ChineseConversionDirection.SimplifiedToTraditional);
            return r;
        }
        /// <summary> 
        /// 繁体转换为简体
        /// </summary> 
        /// <param name="str">繁体字</param> 
        /// <returns>简体字</returns> 
        public static string ToSimplified(this string str)
        {
            var r = ChineseConverter.Convert(str, ChineseConversionDirection.TraditionalToSimplified);
            return r;
        }

        /// <summary>       
        /// 拼音检索         
        /// </summary>         
        /// <param name="strText"></param>         
        /// <returns></returns>         
        public static string GetChineseSpell(string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr += GetSpell(strText.Substring(i, 1));
            }
            return myStr;
        }

        /// <summary>         
        /// 得到首字母         
        /// </summary>         
        /// <param name="cnChar"></param>  
        /// <returns></returns>         
        public static string GetSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };

                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "";
            }
            else
                return cnChar;
        }
    }
}

